<?php
if (!defined('FosfoaSystem')) {exit(1);}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
class news extends general
{
    public function __construct()
    {
        parent::__construct();
        $this->tpl_title = T("Dashboard");
        $this->navigation_current = 'categories';
        $this->categories = spClass('newscategoriesModel')->spLinker()->findAll(null,
            'sort_order asc,categories_id asc');
    }
    function index()
    {
        $this->tt = "信息管理";
        $categories_id = $this->spArgs("categories_id", 0);
        $current_page = $this->spArgs("page", 1);
        $page_size = $this->spArgs("size", 12);
        $obj = spClass("newsModel");
        $obj2 = spClass("news_commentsModel");
        if ($categories_id == 0) {
            $conditions = array('1' => 1);
        } else {
            $conditions = array('master_categories_id' => $categories_id);
        }

        $arr = $obj->spLinker()->spPager($current_page, $page_size)->findAll($conditions,
            'news_id desc');
        foreach ($arr as $k => $v) {
            if ($obj2->ispl($v['news_id'])) {
                $arr[$k]['pl'] = 1;
            }
        }
        $arrBar = $obj->spLinker()->spPager()->getPager();
        $this->tPageBar = $arrBar;
        $this->arr = $arr;
        $this->categories_id = $categories_id;
        $this->display("admin/news_index.html");
    }

    function add()
    {
        $categories_id = $this->spArgs("categories_id", 0);
        $tree = spClass('Tree');
        $fields = array('categories_id', 'parent_id');
        $tree->treelist($this->categories, $fields);
        $arr = $tree->leaf(0);
        $this->arr = $arr;
        $this->action = 'add';
        $this->parent_id = $categories_id;
        $this->addtitle = "添加信息";
        $this->size = array(
            0 => '鞋类',
            1 => '包类',
            2 => '衣服');
        $this->display("admin/news_add.html");
    }

    function edit()
    {
        $this->size = array(
            0 => '鞋类',
            1 => '包类',
            2 => '衣服');
        $news_id = $this->spArgs("news_id", 0);
        $Obj = spClass('newsModel');
        $conditions = array("news_id" => $news_id);
        $news = $Obj->spLinker()->find($conditions);
        $tree = spClass('Tree');
        $fields = array('categories_id', 'parent_id');
        $tree->treelist($this->categories, $fields);
        $arr = $tree->leaf(0);
        $this->arr = $arr;
        $this->news = $news;
        $this->parent_id = $news['master_categories_id'];
        $allimg = unserialize($news['news_allimg']);
        $i = count($allimg) - 1;
        foreach ($allimg as $key => $value) {
            if ($key == $i) {
                $img .= $value;
            } else {
                $img .= $value . '|';
            }
        }
        $this->img = $img;
        $this->allimg = $allimg;
        $this->addtitle = "修改信息";
        $this->display("admin/news_add.html");
    }
    function save()
    {
        $strAction = $this->spArgs("action");
        $allimg = $this->spArgs("allimg");
        if ($allimg <> "") {
            $allimg = explode('|', $allimg);
            $allimg = array_unique($allimg);
            $allimg = serialize($allimg);
        }
        $seokey = $this->spArgs("news_seokeywords");

        $data = array(
            "master_categories_id" => $this->spArgs("parent_id"),
            'alias' => $this->spArgs('alias'),
            'type' => $this->spArgs('type',0),
            "detail" => array(
                "news_name" => $this->spArgs("name"),
                'news_title' => $this->spArgs('title'),
                "news_description" => $this->spArgs("content"),
                "news_seokeywords" => $seokey,
                "news_seodescription" => $this->spArgs("news_seodescription"),
                "news_url" => $this->spArgs("news_url", 0)),
            "news_relate" => $this->spArgs('news_Relate'),
            "sort" => $this->spArgs("sort", 0),
            "news_allimg" => $allimg,
            "news_image" => $this->spArgs("uploadList"));

        $Obj = spClass('newsModel');
        if ($strAction == 'add') {
            $data['news_date_added'] = time();
            $news_id = $Obj->spLinker()->create($data);
            $msg = "信息" . $data[detail][news_name] . "添加成功";
        } else {
            $data['news_last_modified'] = time();
            $news_id = $this->spArgs("id");
            $condition = array('news_id' => $news_id);
            $Obj->spLinker()->update($condition, $data);
            $msg = "信息" . $data[detail][news_name] . "修改成功";
        }
        $tag = spClass('tagModel')->Tagsave($seokey, 1);
        Savelog($msg);
        $result = array(
            'status' => 'y',
            'info' => $msg,
            );
        echo json_encode($result); // 返回（显示）JSON结果
    }
    function del()
    {
        $Obj = spClass("newsModel");
        $Obj2 = spClass('news_commentsModel');
        $id2 = $this->spArgs("id", 0);
        $cid = $this->spArgs("cid", 0);
        if ($id2 == 0) {
            $ids = $this->spArgs("selids");
            $idsarray = explode(',', $ids);
            foreach ($idsarray as $id) {
                $conditions = array('news_id' => $id);
                $arr = $Obj->find($conditions, '', 'news_allimg');
                $allimg = $arr['news_allimg'];

                if ($allimg <> "") {
                    $allimg = unserialize($allimg);
                    foreach ($allimg as $img) {

                        if (file_exists("upload/" . $img)) {
                            @unlink("upload/" . $img);
                        }
                    }
                }
                if ($Obj->spLinker()->delete($conditions)) // 删除记录
                    {
                    $Obj2->delete($conditions); //删除评论
                }
            }
        } else {
            $conditions = array('news_id' => $id2);
            $arr = $Obj->find($conditions, '', 'news_allimg');
            $allimg = $arr['news_allimg'];
            if ($allimg <> "") {
                $allimg = unserialize($allimg);
                foreach ($allimg as $img) {
                    if (file_exists("upload/" . $img)) {
                        @unlink("upload/" . $img);
                    }
                }
            }

            if ($Obj->spLinker()->delete($conditions)) // 删除记录
                {
                $Obj2->delete($conditions); //删除评论
            }
        }
        $msg = "删除新闻信息" . $id2;
        Savelog($msg);
        $this->success(T('Successfully  deleted!'), spUrl("news", "index", array("categories_id" =>
                "$cid")));
    }

    function createRss()
    {
        $news = spClass("newsModel");
        import(SP_PATH . '/Extensions/feedcreator.class.php');
        $rss = new UniversalFeedCreator();
        $rss->useCached();
        $rss->title = SITENAME;
        $rss->description = KEYWORDS;
        $rss->link = WEBURL;
        $rss->generator = STORENAME;
        $rss->syndicationURL = "XXX";
        $image = new FeedImage();
        $image->title = SITENAME;
        $image->url = WEBURL . '/themes/' . __SKIN_NAME . '/images/logo.png';
        $image->link = WEBURL;
        $image->description = KEYWORDS;
        $rss->image = $image;

        // get data items from somewhere, e.g. the database:

        $res = $news->get_news_list(null, RSSNEWS);
        foreach ($res as $k => $v) {
            $reg_tag = '/<img.*?\"([^\"]*(jpg|bmp|jpeg|gif)).*?>/';
            $description = preg_replace($reg_tag, '<img src="' . WEBURL . '/$1" alt="' . $v[detail][news_title] . '"/>', $v[detail][news_description]);
            str_replace('<img src="', '<img src="' . WEBURL . '/', $v[detail][news_description]);
            $item = new FeedItem();
            $item->title = $v[detail][news_name];
            $item->date = $v[news_date_added];
            $item->link = WEBURL . WEBDIR . 'view/'.$v['alias'].'.html';
            $item->description = strip_tags($description, '<p><br><a><img>');
            $category = spClass('newscategoriesModel')->CDetail2($v[master_categories_id]);
            $item->category = $category[detail][categories_name];
            $rss->addItem($item);
        }
        $msg = "创建Feed.xml成功";
        Savelog($msg);
        $rss->saveFeed("RSS2.0", "feed.xml");
        @chmod("feed.xml", 0644);
    }

    function checkalias()
    {
        $id = $this->spArgs('newsid');
        $alias = $this->spArgs('param');
        if ($alias <> '') {
            $aliasobj = spClass('newsModel')->aliascheck($alias, $id);
            if ($aliasobj) {
                echo "y";
            } else {
                echo "存在重复别名,请更换！";
            }
        } else {
            echo "请输入网址别名！";
        }
    }
}